﻿namespace LeetCodeProject._0001_1000._101_200._111_120._113;

public class Solution
{
    public IList<IList<int>> PathSum(TreeNode root, int targetSum)
    {
        var results = new List<IList<int>>();
        var path = new List<int>();
        PathSum(root, targetSum, path, results);
        return results;
    }

    private void PathSum(TreeNode root, int sum, List<int> path, List<IList<int>> results)
    {
        switch (root)
        {
            case null:
                return;
            case var _ when root.left == null && root.right == null:
                if (sum == root.val)
                {
                    path.Add(root.val);
                    results.Add(path.ToArray());
                    path.RemoveAt(path.Count - 1);
                }
                return;
            default:
                path.Add(root.val);
                PathSum(root.left, sum - root.val, path, results);
                PathSum(root.right, sum - root.val, path, results);
                path.RemoveAt(path.Count - 1);
                return;
        }
    }
    
}


